﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TerenXNA
{
    public class WorldObject
    {
        public Model model;
        public Vector3 position;
        public int tileX, tileY;
        Matrix translateMatrix;

        public WorldObject(Model model, Vector3 position, int tileX, int tileY)
        {
            this.model = model;
            this.position = position;
            this.tileX = tileX;
            this.tileY = tileY;
            translateMatrix = Matrix.CreateTranslation((Tile.width - 1) * tileX, 0, (Tile.height - 1) * tileY) * Matrix.CreateScale(1, Game1.heightScale, 1);
        }

        public void draw(Matrix viewMatrix, Matrix projectionMatrix, Camera aktCam)
        {
            //float terrainHeight = terrain.getHeight((int)worldObject.position.X, (int)worldObject.position.Y) * 10;
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    //effect.EnableDefaultLighting();
                    effect.LightingEnabled = true;
                    //effect.PreferPerPixelLighting = true;
                    effect.DirectionalLight0.Direction = Game1.effect.Parameters["xLightDirection"].GetValueVector3();
                    effect.AmbientLightColor = new Vector3(0.35f, 0.35f, 0.35f);

                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                    Vector3 rot = new Vector3(-MathHelper.PiOver2, 0, MathHelper.Pi - MathHelper.PiOver4);
                    //Matrix world = Matrix.CreateFromYawPitchRoll(0, rot.X, 0) * Matrix.CreateTranslation(worldObject.position.Y, 0, worldObject.position.X);

                    //effect.World = world * Matrix.CreateScale(0.1f) * translateMatrix;
                    effect.World = Matrix.CreateScale(0.02f, 0.02f, 0.02f) *
                        Matrix.CreateFromYawPitchRoll(0, rot.X, 0) *
                        translateMatrix *
                        Matrix.CreateTranslation(position.Y - aktCam.coords.aktwx * (Tile.width - 1), position.Z * Game1.heightScale, position.X - aktCam.coords.aktwz * (Tile.height - 1));
                    //Matrix.CreateTranslation(worldObject.position.Y - aktCam.coords.aktwx, worldObject.position.Z * Game1.heightScale, worldObject.position.X - aktCam.coords.aktwz);

                    //effect.Parameters["xLightDirection"].SetValue(World.lightDirection);
                }
                mesh.Draw();
            }
        }
    }
}
